package cn.zyg.controller;

import cn.zyg.domain.User;
import cn.zyg.sentinel.OrderFallbackClass;
import cn.zyg.feign.UserService;
import cn.zyg.sentinel.GetByIdBlockHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")

public class OrderController {
    @Autowired
    private UserService userService;

    @GetMapping("/getById/{id}")
    @SentinelResource(value = "order-getById",
            blockHandler = "getByIdBlockHandler",
            blockHandlerClass = GetByIdBlockHandler.class,
            fallback = "getByIdFallback",
            fallbackClass = OrderFallbackClass.class)
    public User getById(@PathVariable("id") Long id){
//        if (true) {
//            throw new RuntimeException();
//        }
        User user = new User();
        System.out.println(user);
        return new User(-1L, "我没错。错的是User！");
    }

//    public User getByIdFallback(@PathVariable("id") Long id, Throwable e){
//        return new User(-1L, "降级了，服务器异常");
//    }
//    public User getByIdBlockHandler(@PathVariable("id") Long id, BlockException e){
//        e.printStackTrace();
//        return new User(-1L, "服务降级触发, 当前服务流量过大，请稍后重试！");
//    }

}